package com.car.web.Car;

import com.car.domain.Car;
import com.car.server.impl.AdminCarSeverImpl;
import org.apache.commons.beanutils.BeanUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/car/AddCar")
public class AddCar extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //封装
        Car car = new Car();
        try {
            new BeanUtilsBean().populate(car,req.getParameterMap());

            //添加
            int i = new AdminCarSeverImpl().AddCarServer(car);
            if (i!=0){
                //成功 跳转
                req.getSession().setAttribute("add","添加成功");
            }else {
                req.getSession().setAttribute("add","添加失败");
            }
            //重定向
            resp.sendRedirect("InquireMyCar");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
